package com.manli.api.service;

import com.manli.api.pojo.SessionInfo;
import com.manli.api.util.SessionInfoUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.WeChatUserInfoAddressDao;
import com.manli.api.dto.WeChatUserInfoAddress;

import javax.servlet.http.HttpServletRequest;


@Service("weChatUserInfoAddressService")
public class WeChatUserInfoAddressService {
    @Autowired
    WeChatUserInfoAddressDao weChatUserInfoAddressDao;
    @Autowired
    SQLManager sqlManager;
    @Autowired
    SessionInfoUtils sessionInfoUtils;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from weChatUserInfoAddress where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<WeChatUserInfoAddress> pageQuery = new PageQuery<WeChatUserInfoAddress>(params.getPage(),params.getLimit());
        PageQuery<WeChatUserInfoAddress> query =sqlManager.execute(sqlReady,WeChatUserInfoAddress.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<WeChatUserInfoAddress> list = weChatUserInfoAddressDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer weChatUserInfoAddressId) {
        WeChatUserInfoAddress weChatUserInfoAddress = weChatUserInfoAddressDao.single(weChatUserInfoAddressId);
        return ResultUtil.success(weChatUserInfoAddress);
    }

    @Transactional
    public Result save(WeChatUserInfoAddress weChatUserInfoAddress) {
        weChatUserInfoAddressDao.insert(weChatUserInfoAddress);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(WeChatUserInfoAddress weChatUserInfoAddress) {
        weChatUserInfoAddressDao.updateTemplateById(weChatUserInfoAddress);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(WeChatUserInfoAddress weChatUserInfoAddress) {
        weChatUserInfoAddressDao.updateTemplateById(weChatUserInfoAddress);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            weChatUserInfoAddressDao.deleteById(id);
        }
        return ResultUtil.success();
    }

    @Transactional
    public Result getWeChatUserInfoAddress(HttpServletRequest request) {
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        List<WeChatUserInfoAddress> addresses = weChatUserInfoAddressDao.createLambdaQuery()
                .andEq("unionid",sessionInfo.getWeChatUserInfo().getUnionid())
                .andEq("status",1)
                .appendSql(" order by default_address desc ")
                .select();
        return ResultUtil.success(addresses);
    }

    @Transactional
    public Result addWeChatUserInfoAddress(WeChatUserInfoAddress weChatUserInfoAddress, HttpServletRequest request) {
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        weChatUserInfoAddress.setUnionid(sessionInfo.getWeChatUserInfo().getUnionid());
        weChatUserInfoAddress.setOpenid(sessionInfo.getWeChatUserInfo().getOpenid());
        weChatUserInfoAddress.setStatus(1);
        //判断是否设为默认地址
        List<WeChatUserInfoAddress> list = weChatUserInfoAddressDao.createLambdaQuery()
                .andEq("unionid",sessionInfo.getWeChatUserInfo().getUnionid()).select();
        if(list.size() == 0){
            weChatUserInfoAddress.setDefaultAddress(1);
        }
        weChatUserInfoAddressDao.insert(weChatUserInfoAddress);
        return ResultUtil.success("地址新增成功");
    }

    @Transactional
    public Result updateWeChatUserInfoAddress(WeChatUserInfoAddress weChatUserInfoAddress, HttpServletRequest request) {
        //判断是否设为默认地址
        weChatUserInfoAddressDao.updateTemplateById(weChatUserInfoAddress);
        return ResultUtil.success("地址更新成功");
    }

    @Transactional
    public void setDefaultWeChatUserInfoAddress(WeChatUserInfoAddress weChatUserInfoAddress , HttpServletRequest request){
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        //设置默认地址
        if(weChatUserInfoAddress.getDefaultAddress() == 1){
            weChatUserInfoAddressDao.executeUpdate("update we_chat_user_info_address set default_address = 0 where unionid=? ",sessionInfo.getWeChatUserInfo().getUnionid());
        }
    }

    @Transactional
    public Result getSingleWeChatUserInfoAddress(WeChatUserInfoAddress weChatUserInfoAddress , HttpServletRequest request) {
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        WeChatUserInfoAddress addresses = weChatUserInfoAddressDao.createLambdaQuery()
                .andEq("unionid",sessionInfo.getWeChatUserInfo().getUnionid())
                .andEq("we_chat_user_info_address_id" , weChatUserInfoAddress.getWeChatUserInfoAddressId())
                .single();
        return ResultUtil.success(addresses);
    }

    @Transactional
    public Result updateWeChatUserInfoAddressSetDefault(WeChatUserInfoAddress weChatUserInfoAddress, HttpServletRequest request) {
        setDefaultWeChatUserInfoAddress(weChatUserInfoAddress,request);
        weChatUserInfoAddressDao.executeUpdate(
                "update we_chat_user_info_address set default_address = 1 where we_chat_user_info_address_id = ? "
                ,weChatUserInfoAddress.getWeChatUserInfoAddressId());
        return ResultUtil.success("默认地址更新成功");
    }
}
